let a = null;
// console.log(a);
let b = null;
initArtCateList()

function initArtCateList() {
    $.ajax({
        type: 'get',
        url: '/my/article/cates',
        success: (res) => {
            console.log(res)
            let htmls = template('template1', res)
            $('tbody').html(htmls)
        }
    })
}

$(".layui-btn").on("click", function () {
    a = layui.layer.open({
        type: '1',
        title: '添加',
        area: ['500px', '250px'],
        content: $('#di-add').html()
    })

})

$("body").on("submit", ".layui-form", function (e) {
    e.preventDefault()
    $.ajax({
        type: 'post',
        url: '/my/article/addcates',
        data: $(this).serialize(),
        success: (res) => {
            if (res.status != 0) return layui.layer.msg('添加失败')

            layui.layer.msg('添加成功')
            initArtCateList()
            layui.layer.close(a)
        }
    })
})



$("tbody").on("click", ".btn-edit", function () {
    b = layui.layer.open({
        type: '1',
        title: '修改',
        area: ['500px', '250px'],
        content: $('#di-add1').html()
    })
    let id = $(this).attr('data-id')
    console.log(id)
    $.ajax({
        type: 'get',
        url: '/my/article/cates/' + id,
        success: (res) => {
            console.log(res)

            layui.form.val("form-m", res.data)
        }
    })
    // 
})
$('tbody').on('submit', '.form-add1', function (e) {
    e.preventDefault()
    $.ajax({
        type: 'post',
        url: '/my/article/updatecate',
        data: $(this).serialize(),
        success: (res) => {
            if (res.status !== 0) return layui.layer.msg('失败')

            layui.layer.msg('成功')
            initArtCateList()
            layui.layer.close(b)
        }
    })
})


$('tbody').on("click", ".btn-del", function () {
    //获取自定义属性
    let id = $(this).attr('data-id')
    layui.layer.confirm('确认删除?', {
        icon: 3,
        title: '提示'
    }, function (index) {
        $.ajax({
            type: "get",
            url: '/my/article/deletecate/' + id,
            success: (res) => {
                if (res.status != 0) return layui.layer.msg('添加失败')

                initArtCateList()
                layui.layer.close(index)
            }
        })
    })
})